package com.asd.map.service.impl;

import com.asd.map.entity.*;
import com.asd.map.entity.vo.Line_surfaceVo;
import com.asd.map.mapper.InformationMapper;
import com.asd.map.mapper.Line_styleMapper;
import com.asd.map.mapper.Line_surfaceMapper;
import com.asd.map.mapper.Surface_styleMapper;
import com.asd.map.service.ILine_surfaceService;
import com.asd.system.mapper.ImagesMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @createtime 2021-09-02 9:49
 * @description
 */
@Service
public class line_surfaceServiceImpl extends ServiceImpl<Line_surfaceMapper, Line_surface> implements ILine_surfaceService {

    @Autowired
    private Line_surfaceMapper line_surfaceMapper;

    @Autowired
    private InformationMapper informationMapper;

    @Autowired
    private Line_styleMapper line_styleMapper;

    @Autowired
    private Surface_styleMapper surface_styleMapper;

    @Autowired
    private ImagesMapper imagesMapper;


    @Override
    public List<Line_surface> selectInformationPage(Page<Line_surface> page) {
        QueryWrapper<Line_surface> queryWrapper = new QueryWrapper<>();
        //  判断年龄大于20岁的，这里用的是mybatis-plus的条件构造器
//        queryWrapper.gt("age",20);
        /**
         *    加上false就不查询总记录数啦,如果不需要的话可以加上false，这样就会少一条sql语句
         *   Page<Student> page = new Page<>(current,size,false);
         */
//        Page<Information> page1 = new Page<>();
        //   queryWrapper  可以为null  不进行条件判断
        //  返回list 调用  selectPage
        IPage<Line_surface> informationIPage = line_surfaceMapper.selectPage(page, queryWrapper);


//        System.out.println("总页数" + informationIPage.getPages());
//        System.out.println("总记录数" + informationIPage.getTotal());
        //getRecords()分页对象记录列表
        List<Line_surface> records = informationIPage.getRecords();
        return records;
    }

    @Override
    public Line_surfaceVo findById(String id) {

        Line_surface line_surface = line_surfaceMapper.selectById(id);

        Line_surfaceVo line_surfaceVo = new Line_surfaceVo();

        List<String> viceEchoPathlist =  imagesMapper.findViceEchoPathByPid(id);

        List<String> primaryEchoPathlist =  imagesMapper.findprimaryEchoPathByPid(id);


        line_surfaceVo.setId(line_surface.getId());
        line_surfaceVo.setDid(id);
        line_surfaceVo.setData(line_surface.getData());
        line_surfaceVo.setLineSurfaces(line_surface.getLineSurfaces());
        line_surfaceVo.setPrimaryEchoPathlist(primaryEchoPathlist);
        line_surfaceVo.setViceEchoPathlist(viceEchoPathlist);

        return line_surfaceVo;
    }

    @Override
    public List<Line_surfaceVo> findByDid1(Page<Line_surfaceVo> page, String did) {
        List<Line_surfaceVo> line_surfaceList = line_surfaceMapper.findByDid1(page,did);

        List<Line_surfaceVo> voList = new ArrayList<>();


        for (int i=0;i<line_surfaceList.size();i++){
            Line_surfaceVo line_surface = line_surfaceList.get(i);
            String id = line_surface.getId();

            Line_surfaceVo line_surfaceVo = new Line_surfaceVo();

            List<String> viceEchoPathlist =  imagesMapper.findViceEchoPathByPid(id);

            List<String> primaryEchoPathlist =  imagesMapper.findprimaryEchoPathByPid(id);

            line_surfaceVo.setId(line_surface.getId());
            line_surfaceVo.setDid(id);
            line_surfaceVo.setData(line_surface.getData());
            line_surfaceVo.setLineSurfaces(line_surface.getLineSurfaces());
            line_surfaceVo.setPrimaryEchoPathlist(primaryEchoPathlist);
            line_surfaceVo.setViceEchoPathlist(viceEchoPathlist);

            voList.add(line_surfaceVo);
        }


        return voList;
    }

    @Override
    public List<Line_surfaceVo> getByDid(String applyId,String did) {

        List<Line_surface> line_surfaceList = line_surfaceMapper.getByDid(did);

        Information information = informationMapper.selectById(did);

        List<Line_surfaceVo> line_surfaceVos = new ArrayList<>();

        for (int i=0;i<line_surfaceList.size();i++){

            Line_surface line_surface = line_surfaceList.get(i);
            String pid = line_surface.getId();

            List<String> viceEchoPathlist =  imagesMapper.findViceEchoPathByPid(pid);

            List<String> primaryEchoPathlist =  imagesMapper.findprimaryEchoPathByPid(pid);

            Line_surfaceVo line_surfaceVo = new Line_surfaceVo();

            line_surfaceVo.setId(line_surface.getId());
            line_surfaceVo.setDid(did);
            line_surfaceVo.setData(line_surface.getData());
            line_surfaceVo.setLineSurfaces(line_surface.getLineSurfaces());
            line_surfaceVo.setPrimaryEchoPathlist(primaryEchoPathlist);
            line_surfaceVo.setViceEchoPathlist(viceEchoPathlist);


            if (information.getType().equals("1")){//线数据时

                List<Line_style> styleList = line_styleMapper.findByApplyId(applyId, did);

                for (Line_style line_style:styleList) {

                    if (line_style.getType().equals("0")){

                        line_surfaceVo.setLine_style(line_style);

                    }else {
                        if (line_surface.getId().equals(line_style.getLid())){
                            line_surfaceVo.setLine_style(line_style);
                        }
                    }
                }

            }else if (information.getType().equals("2")){//面数据时

                List<Surface_style> styleList = surface_styleMapper.findByApplyId(applyId, did);

                for (Surface_style surface_style:styleList) {

                    if (surface_style.getType().equals("0")){

                        line_surfaceVo.setSurface_style(surface_style);

                    }else {
                        if (line_surface.getId().equals(surface_style.getLid())){

                            line_surfaceVo.setSurface_style(surface_style);
                        }
                    }
                }

            }



            line_surfaceVos.add(line_surfaceVo);
        }

        return line_surfaceVos;
    }

    @Override
    public void deleteByDid(String did) {
        List<Line_surface> line_surfaceList = line_surfaceMapper.getByDid(did);

        for (int i = 0; i < line_surfaceList.size(); i++) {

            line_surfaceMapper.deleteByDid(did);
        }
    }

    @Override
    public IPage<Line_surface> selectPage(Page<Line_surface> page, Line_surface line_surface) {
        //显示分页查询列表
        QueryWrapper<Line_surface> queryWrapper = new QueryWrapper<>();

        //2.分页查询
        if(line_surface == null){ //没有查询条件 直接返回分页
            return baseMapper.selectPage(page,queryWrapper);
        }

        //3.条件查询
        if(!StringUtils.isEmpty(line_surface.getData())){ //data不为空
            queryWrapper.like("data", line_surface.getData());
        }


        Page<Line_surface> pageList = baseMapper.selectPage(page, queryWrapper);

//        System.out.println(queryWrapper);
        return pageList;
    }

    @Override
    public List<Line_surface> queryByData(String data) {
        return line_surfaceMapper.queryByData(data);
    }
}
